Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | import { NextResponse } from 'next/server' import { banUserFromRoom, getRoomBans, unbanUserFromRoom } from '@/lib/arcade/room-moderation' import { getRoomMembers } from '@/lib/arcade/room-membership' import { getRoomActivePlayers } from '@/lib/arcade/player-manager' import { getUserRoomHistory } from '@/lib/arcade/room-member-history' import { createInvitation } from '@/lib/arcade/room-invitations' import { withAuth } from '@/lib/auth/withAuth' import { getUserId } from '@/lib/viewer' import { getSocketIO } from '@/lib/socket-io' /** * POST /api/arcade/rooms/:roomId/ban * Ban a user from the room (host only) * Body: * - userId: string * - reason: string (enum) * - notes?: string (optional) */ export const POST = withAuth(async (request, { params }) => { try { const { roomId } = (await params) as { roomId: string } const userId = await getUserId() const body = await request.json() // Validate required fields if (!body.userId || !body.reason) { return NextResponse.json( { error: 'Missing required fields: userId, reason' }, { status: 400 } ) } // Validate reason const validReasons = ['harassment', 'cheating', 'inappropriate-name', 'spam', 'afk', 'other'] if (!validReasons.includes(body.reason)) { return NextResponse.json({ error: 'Invalid reason' }, { status: 400 }) } // Check if user is the host const members = await getRoomMembers(roomId) const currentMember = members.find((m) => m.userId === userId) if (!currentMember) { return NextResponse.json({ error: 'You are not in this room' }, { status: 403 }) } if (!currentMember.isCreator) { return NextResponse.json({ error: 'Only the host can ban users' }, { status: 403 }) } // Can't ban yourself if (body.userId === userId) { return NextResponse.json({ error: 'Cannot ban yourself' }, { status: 400 }) } // Get the user to ban (they might not be in the room anymore) const targetUser = members.find((m) => m.userId === body.userId) const userName = targetUser?.displayName || body.userId.slice(-4) // Ban the user await banUserFromRoom({ roomId, userId: body.userId, userName, bannedBy: userId, bannedByName: currentMember.displayName, reason: body.reason, notes: body.notes, }) // Broadcast updates via socket const io = await getSocketIO() if (io) { try { // Get updated member list const updatedMembers = await getRoomMembers(roomId) const memberPlayers = await getRoomActivePlayers(roomId) // Convert memberPlayers Map to object for JSON serialization const memberPlayersObj: Record<string, any[]> = {} for (const [uid, players] of memberPlayers.entries()) { memberPlayersObj[uid] = players } // Tell the banned user they've been removed io.to(`user:${body.userId}`).emit('banned-from-room', { roomId, bannedBy: currentMember.displayName, reason: body.reason, }) // Notify everyone else in the room io.to(`room:${roomId}`).emit('member-left', { roomId, userId: body.userId, members: updatedMembers, memberPlayers: memberPlayersObj, reason: 'banned', }) console.log(`[Ban API] User ${body.userId} banned from room ${roomId}`) } catch (socketError) { console.error('[Ban API] Failed to broadcast ban:', socketError) } } return NextResponse.json({ success: true }, { status: 200 }) } catch (error: any) { console.error('Failed to ban user:', error) return NextResponse.json({ error: 'Failed to ban user' }, { status: 500 }) } }) /** * DELETE /api/arcade/rooms/:roomId/ban * Unban a user from the room (host only) * Body: * - userId: string */ export const DELETE = withAuth(async (request, { params }) => { try { const { roomId } = (await params) as { roomId: string } const userId = await getUserId() const body = await request.json() // Validate required fields if (!body.userId) { return NextResponse.json({ error: 'Missing required field: userId' }, { status: 400 }) } // Check if user is the host const members = await getRoomMembers(roomId) const currentMember = members.find((m) => m.userId === userId) if (!currentMember) { return NextResponse.json({ error: 'You are not in this room' }, { status: 403 }) } if (!currentMember.isCreator) { return NextResponse.json({ error: 'Only the host can unban users' }, { status: 403 }) } // Unban the user await unbanUserFromRoom(roomId, body.userId) // Auto-invite the unbanned user back to the room const history = await getUserRoomHistory(roomId, body.userId) if (history) { const invitation = await createInvitation({ roomId, userId: body.userId, userName: history.displayName, invitedBy: userId, invitedByName: currentMember.displayName, invitationType: 'auto-unban', message: 'You have been unbanned and are welcome to rejoin.', }) // Broadcast invitation via socket const io = await getSocketIO() if (io) { try { io.to(`user:${body.userId}`).emit('room-invitation-received', { invitation: { id: invitation.id, roomId: invitation.roomId, invitedBy: invitation.invitedBy, invitedByName: invitation.invitedByName, message: invitation.message, createdAt: invitation.createdAt, invitationType: 'auto-unban', }, }) console.log( `[Unban API] Auto-invited user ${body.userId} after unban from room ${roomId}` ) } catch (socketError) { console.error('[Unban API] Failed to broadcast invitation:', socketError) } } } return NextResponse.json({ success: true }, { status: 200 }) } catch (error: any) { console.error('Failed to unban user:', error) return NextResponse.json({ error: 'Failed to unban user' }, { status: 500 }) } }) /** * GET /api/arcade/rooms/:roomId/ban * Get all bans for a room (host only) */ export const GET = withAuth(async (_request, { params }) => { try { const { roomId } = (await params) as { roomId: string } const userId = await getUserId() // Check if user is the host const members = await getRoomMembers(roomId) const currentMember = members.find((m) => m.userId === userId) if (!currentMember) { return NextResponse.json({ error: 'You are not in this room' }, { status: 403 }) } if (!currentMember.isCreator) { return NextResponse.json({ error: 'Only the host can view bans' }, { status: 403 }) } // Get all bans const bans = await getRoomBans(roomId) return NextResponse.json({ bans }, { status: 200 }) } catch (error: any) { console.error('Failed to get bans:', error) return NextResponse.json({ error: 'Failed to get bans' }, { status: 500 }) } }) |